package com.carck.controller;

import com.carck.config.ServerConfig;
import com.carck.pojo.Group;
import com.carck.pojo.Model;
import com.carck.pojo.Pose;
import com.carck.pojo.Result;
import com.carck.service.ModelService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.Arrays;

@Slf4j
@RestController
public class RenderController {
    @Autowired
    private ModelService modelService;
//    private String sceneName;
    @PostMapping("/getModel")
    @CrossOrigin
    public Result getModel(@RequestBody Model model) throws MalformedURLException {
//        sceneName=model.getGroupName()+model.getModelName();
        //查询数据库TODO
        String modelURL=modelService.getFileURL(model);
//        if(model.getModelId()==0)
//            modelURL="http://192.168.1.10"+":"+"8080"+"/"+"test.ply";
//        else if (model.getModelId()==1)
//            modelURL="http://192.168.1.10"+":"+"8080"+"/"+"out.ply";
        //返回结果
        return Result.success(modelURL);
    }
    @GetMapping("/availableModel")
    public Result availableModel()
    {
        ArrayList<Group> out=modelService.list();
        return Result.success(out);
//        return Result.success(new ArrayList<Group>(Arrays.asList(
//                new Group("分组一",
//                        new ArrayList<String>(Arrays.asList("模型1","模型2"))))
//                )
//        );
    }
    @PostMapping("/renderPose")
    public Result renderPose(@RequestBody Pose pose) throws IOException, InterruptedException {

        String getRenderingCommand = "/home/carbon/anaconda3/bin/python3.7 "+"/home/carbon/IdeaProjects/carck/src/main/resources/python_script/getnerf/getRendering.py --scene_name "+pose.getModelName()+" " +
                "--lookatx " +pose.getFront().getX()
                +" --lookaty "+pose.getFront().getY()
                +" --lookatz "+pose.getFront().getZ()
                +" --tranx "+pose.getTran().getX()
                +" --trany "+pose.getTran().getY()
                +" --tranz "+pose.getTran().getZ()
                ;
        log.info(getRenderingCommand);
        Process process2 = Runtime.getRuntime().exec(getRenderingCommand);
        process2.waitFor();
        String ipAddr= ServerConfig.getIPAdress();
        String port=ServerConfig.getPort();
        return Result.success("http://"+ipAddr+":"+port+"/out.png"
        );
    }
}
